package com.codingfuture.glaciercommon.util;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.ParameterizedType;

/**
 * @Author: Sun
 * @DateTime: 2025/4/1 10:31
 * @Description:
 **/

/**
 * DTO转实体
 * @param <T> 实体类型
 *           DTO要继承该类并提供实体泛型
 */
public abstract class BaseParamDTO<T> {
    private final Class<T> tClass;

    protected BaseParamDTO() {
        this.tClass = (Class<T>) ((ParameterizedType)
                this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public T convert2Entity() {
        T entity = null;
        try {
            entity = tClass.newInstance();
            BeanUtils.copyProperties(this, entity);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return entity;
    }
}
